package com.cn.web.app.template.controller;

import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.annoation.Logs;
import com.cn.msg.ResultMsg;
import com.cn.page.PageVo;
import com.cn.upload.base.FileItemEntity;
import com.cn.user.UserDO;
import com.cn.util.AuthUserContext;
import com.cn.util.BaseController;
import com.cn.util.DateUtil;
import com.cn.web.app.baidu.FastDfsFileUpload;
import com.cn.web.app.template.entity.OrderTemplateEntity;
import com.cn.web.app.template.service.OrderTemplateService;
import com.cn.web.app.template.vo.OrderTemplateListVO;
import com.cn.web.app.template.vo.OrderTemplateMarkEdit;
import com.cn.web.app.template.vo.OrderTemplateSearch;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * description: OrderTemplateController <br>
 * date: 24.7.9 17:25 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping(value = "admin/order/template")
@Api(tags = "票据模板中心")
@Slf4j
public class OrderTemplateController extends BaseController {
    @Autowired
    private OrderTemplateService orderTemplateService;

    @Autowired
    private FastDfsFileUpload fastDfsFileUpload;

    @ApiOperation(value = "票据模板列表")
    @PostMapping(value = "page")
    public ResultMsg<PageInfo<OrderTemplateListVO>> page(
            @RequestBody PageVo<OrderTemplateSearch> pageVo
    ) {
        OrderTemplateSearch search = pageVo.getParam();
        UserDO token = AuthUserContext.get();
        if (token.getSuperFlag().equalsIgnoreCase("1")) {
            search.setUserId(token.getId().toString())
                    .setType(1);
        }
        return ResultMsg.builder(this.orderTemplateService.page(pageVo));
    }

    @Logs(value = "编辑", desc = "票据模板中心")
    @ApiOperation(value = "编辑")
    @PostMapping(value = "edit")
    public ResultMsg edit(
            @RequestBody OrderTemplateEntity info
    ) {
        UserDO token = AuthUserContext.get();
        this.orderTemplateService.edit(info, token);

        return ResultMsg.builder();
    }

    @Logs(value = "删除", desc = "票据模板中心")
    @ApiOperation(value = "删除")
    @GetMapping(value = "del")
    public ResultMsg del(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        this.orderTemplateService.del(id);
        return ResultMsg.builder();
    }

    @Logs(value = "审批", desc = "票据模板中心")
    @ApiOperation(value = "审批")
    @PostMapping(value = "approve")
    public ResultMsg approve(
            @RequestBody OrderTemplateEntity info
    ) {
        this.orderTemplateService.approve(info);
        return ResultMsg.builder();
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "findVo")
    public ResultMsg<OrderTemplateListVO> findVO(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        return ResultMsg.builder(this.orderTemplateService.findVO(id));
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "find")
    public ResultMsg<OrderTemplateEntity> find(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        return ResultMsg.builder(this.orderTemplateService.find(id));
    }

    @ApiOperation(value = "处理")
    @PostMapping(value = "handle")
    public ResultMsg handle(
            @RequestBody OrderTemplateMarkEdit info
    ) {
        this.orderTemplateService.handle(info);
        return ResultMsg.builder();
    }

    @ApiOperation(value = "获取本机构下的模板列表")
    @GetMapping(value = "listTemplate")
    public ResultMsg listTemplate() {
        UserDO token = AuthUserContext.get();
        OrderTemplateSearch search = new OrderTemplateSearch();
        if (token.getOrgs() != null && token.getOrgs().size() > 0) {
            search.setOrgId(token.getOrgs().get(0).getId().toString()
            );
        } else {
            search.setOrgId("1");
        }
        search.setStatus(1);
        search.setType(2).setTemplateType(0);
        List<OrderTemplateListVO> orders = this.orderTemplateService.list(search);
        return ResultMsg.builder(orders);
    }

    @PostMapping(value = "upload")
    public ResultMsg upload(
            @ApiParam(name = "file") @RequestPart MultipartFile file
    ) {
        Map<String, Object> paramMap = new java.util.HashMap<>();
        try {
            InputStreamResource isr = new InputStreamResource(file.getInputStream(),
                    file.getOriginalFilename());
            paramMap.put("file", isr);

            FileItemEntity fileItemEntity = this.fastDfsFileUpload.upload(paramMap);
            return ResultMsg.builder().setData(fileItemEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultMsg.builder();
    }

}
